package org.luo.news.domain.category;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.luo.common.common.Result;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Override
    public Result add(Category category) {
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());

        category.setCreateUser(1);
        save(category);
        return Result.success(category);
    }

    @Override
    public Result<List<Category>> getList() {
        List<Category> list = lambdaQuery().list();
        return Result.success(list);
    }

    @Override
    public Result<Category> detail(Integer id) {
        Category category = getById(id);
        return Result.success(category);
    }

    @Override
    public Result update(Category category) {
        lambdaUpdate()
                .set(category.getCategoryName() !=null, Category::getCategoryName, category.getCategoryName())
                .set(category.getCategoryAlias() != null, Category::getCategoryAlias, category.getCategoryAlias())
                .set(category.getCreateUser() != null, Category::getCreateUser, category.getCreateUser())
                .eq(true, Category::getId, category.getId())
                .update();
        return Result.success("更新成功");
    }

    @Override
    public Result delete(Integer id) {
        removeById(id);
        return Result.success("删除成功");
    }
}
